package cn.kimoon.dau_android;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.kaopiz.kprogresshud.KProgressHUD;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import cn.kimoon.dau_android.API.ApiDAU;
import cn.kimoon.dau_android.API.AppClient;
import cn.kimoon.dau_android.Model.ModelLogin;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by xiangdong on 2016/12/7.
 */

public class ProcessPushActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "ProcessPushActivity";

    private String id;      // 服务器推送过来的ID
    private String type;    // 服务器推送过来的消息类型Type

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_process_push);

        initSubViews();
    }

    private void initSubViews() {
        TextView textView = (TextView) findViewById(R.id.push_text_view);
        Intent intent = getIntent();
        if (null != intent) {
            Bundle bundle = getIntent().getExtras();
            try {
                JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                textView.setText(json.getString("content"));
                id = json.getString("id");
                type = json.getString("type");
            } catch (JSONException e) {
                Log.e(TAG, "Get message extra JSON error!");
            }
        }

        Button button = (Button) findViewById(R.id.push_get_button);
        button.setOnClickListener(this);
    }

    // 按键处理
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.push_get_button:
                getKnowButton();
                break;
        }
    }

    private void getKnowButton() {
        // 添加网络处理
        requestConfirmMsg(id, type);
    }

    // 网络处理
    private void requestConfirmMsg(String id, String type) {
        final KProgressHUD hud = KProgressHUD.create(this, KProgressHUD.Style.SPIN_INDETERMINATE).show();

        Map<String, String> parameters = new HashMap<>();
        parameters.put("id", id);
        parameters.put("type", type);

        ApiDAU apiDAU = AppClient.retrofit().create(ApiDAU.class);
        Call<ModelLogin> call = apiDAU.apiConfirmMsg(parameters);
        call.enqueue(new Callback<ModelLogin>() {
            @Override
            public void onResponse(Call<ModelLogin> call, Response<ModelLogin> response) {
                hud.dismiss();
                if (response.body() != null && response.body().errCode == 200) {
                    Toast.makeText(ProcessPushActivity.this, response.body().msg, Toast.LENGTH_SHORT).show();
                    // 界面消失
                    finish();
                } else {
                    String msg = (response.body() == null ? response.message() : response.body().msg);
                    Toast.makeText(ProcessPushActivity.this, msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ModelLogin> call, Throwable t) {
                hud.dismiss();
                Toast.makeText(ProcessPushActivity.this, R.string.internet_request_failed, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
